package com.utils;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("image/jpeg");
		String sKichThuoc = request.getParameter("kt");
		if (!StringTool.isNumberic(sKichThuoc)) sKichThuoc = "2";
		try  
        {  
            //Create an image 170 x 20  
            BufferedImage bufferedImage = new BufferedImage(170, 50, BufferedImage.TYPE_INT_RGB);  
            long kt0 = Long.parseLong(sKichThuoc);
            long kt = Long.parseLong(sKichThuoc);
            kt -= 2;
            //Draw an oval  
            Graphics g = bufferedImage.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, 170, 48);
            int startX = 10;
            // |_____|_____|
            g.setColor(Color.black);
            int count = 0;
            g.drawLine(startX-5, 40, startX-5, 49);
            for (int i = 0; i < 200; count++) {
            	if (count % 10 == 0) {
            		if (kt == kt0) g.setColor(Color.red);
            		else g.setColor(Color.black);
            		g.fillRect(i+startX, 30, 2, 19);
            		if (count % 20 == 0) {
            			g.drawString(""+kt, i+startX-5, 20);
            		}
            		g.setColor(Color.black);
            		kt++;
            	} else {
            		g.drawLine(i+startX+1, 40, i+startX+1, 49);
            	}
            	i += 5; 
            }
            
            //Free graphic resources  
            g.dispose();  
  
            //Write the image as a jpg  
            ImageIO.write(bufferedImage, "jpg", response.getOutputStream());  
        }  
        catch (IOException ioe)  
        {  
  
        } 
	}

}
